package com.liushao.controller;


import com.liushao.result.Result;
import com.liushao.result.Result;
import com.liushao.service.MsmService;
import com.liushao.service.MsmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @program: springcloudalibabafinance
 * @ClassName ApiSmsController
 * @description:
 * @author: 陈修华
 * @create: 2025−01-08 15:18
 * @Version 1.0
 **/
@Api(tags = "短信管理")
@RestController
@RequestMapping("/api/sms")
public class ApiSmsController {


    //  http://localhost/api/sms/send/18698789339

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MsmService msmService;

    @ApiOperation("获取验证码功能")
    @GetMapping("/send/{mobile}")
    public Result send(
            @ApiParam(value = "手机号",required = true)
            @PathVariable("mobile") String mobile
    ){
        int num = verificationCode();
        String code = num+"";//待会你们解决一下 自动生成六位数    当年 生成过cardNo
        System.out.println("短信发送成功！"+mobile);
        //发短信
        msmService.send(mobile,code);
        stringRedisTemplate.opsForValue().set("duanxin:"+mobile,code,2, TimeUnit.MINUTES);

        return Result.ok().message("短信发送成功了!");

    }
    private int verificationCode(){
        Random r = new Random();
        return r.nextInt(899999)+100000;
    }
}
